home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/awk -f
- # -*- awk -*-
- #
- # $Header: /usr/bfr/src/test/RCS/fts-f2si.awk,v 1.1 1995/01/18 17:39:14 abel Exp $
- #
- #********************************************
- #
- # FORTRAN to si conversion tool
- #
- #********************************************
- #
- # Written by Alexander L. Belikoff, 1994
- # Copyright (C)1994 Alexander L. Belikoff
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- #********************************************
- #
- # $Log: fts-f2si.awk,v $
- # Revision 1.1 1995/01/18 17:39:14 abel
- # Initial revision
- #
- #
- #********************************************
-
-
- ### Global variable descriptions:
- ### param_names[1..nfargs] : parameters in order.
- ### param_nums : defined by param_nums[param_names[i]] = i
- ### i.e. - inverse of param_names.
- ### locals[1..nlocals] : local variables.
- ### includes[1..nincs] : include files.
- ### externals[ename] : externals.
- ### var_type[varname] : Types of variables.
- ### var_dimen[varname] : dimensions of variables.
- ### common[cname] : common variables.
- ### const_names[1..nconsts] : Names of constants (parameters)
- ### const_vals[1..nconsts] : Values of constants (parameters)
-
- # utility to print rest of function's info
-
- function print_arg(var) {
- if (var in var_type) {
- printf "\n (%-10s %-10s %s)", var, var_type[var], var_dimen[var]
- }
- else {
- printf "\n (%-10s %-10s %s)", var, "%UNKNOWN%", var_dimen[var]
- }
- }
-
- function print_inc(inc) {
- printf "\n (%s)", inc
- }
-
- function print_ext(var) {
- printf "\n (%s)", var
- }
-
- function print_fcn() {
-
- # If there's nothing to print, just return...
- if (nfargs == 0 && MODULE == "" && fcn_name == "" && nincs==0 &&
- nconsts==0) {
-
- empty = 0
- for (s in param_nums) { empty = 1 ; break }
- for (s in externals) { empty = 1 ; break }
- for (s in var_type) { empty = 1 ; break }
- for (s in common) { empty = 1 ; break }
- if (empty == 1) return
- }
-
-
- # output function/common block name
-
- if (fcn_name == "") {
- printf "(*undefined* "
- }
- else {
- printf "(%s ", toupper(fcn_name)
- }
-
- # output module
-
- if (MODULE != "")
- printf "%s ", MODULE
- else
- printf "*undefined* "
-
-
- # output function type
-
- if (is_fn)
- print toupper(fn_type)
- else
- print "*void*"
-
- # parameters info
-
- printf ";;; Arguments:\n"
- printf " ("
-
- for (i = 1 ; i <= nfargs ; i++) {
- print_arg(param_names[i])
- }
- print ")\n"
-
- # callees
-
- printf ";;; Calls:\n"
- printf " ()\n"
-
- # Local variables
-
- printf ";;; Local variables:\n"
- printf " ("
-
- for (var in var_type) {
- if (! (var in param_nums)) print_arg(var)
- }
- print ")\n"
-
-
- # Include files
- printf ";;; Includes:\n"
- printf " ("
- for (i=1; i<=nincs ; i++) {
- print_inc(includes[i])
- }
- print ")\n"
-
- # Externals
- printf ";;; Externals:\n"
- printf " ("
- for (ext in externals) {
- print_ext(ext)
- }
- print ")\n"
-
- # Common blocks
- printf ";;; Common blocks:\n"
- printf " ("
- for (cb in common) {
- printf "\n (%-10s (%s))", cb, common[cb]
- }
- print ")\n"
-
- # Parameters
- printf ";;; Parameters:\n"
- printf " ("
- for (i=1; i<=nconsts; i++) {
- printf "\n (%-10s \042%s\042)", const_names[i], const_vals[i]
- }
- print ")\n"
-
- # close sexp
-
- print ")\n"
- }
-
-
- ### function print_common() {
- ###
- ### # output common block name
- ###
- ### printf "(%s ", toupper(cbname)
- ###
- ### # output module
- ###
- ### if (MODULE != "")
- ### printf "%s ", MODULE
- ### else
- ### printf "*undefined* "
- ###
- ###
- ### # output function type
- ###
- ### print "COMMON"
- ###
- ### # parameters info
- ###
- ### printf ";;; Block elements:\n"
- ### printf " ("
- ###
- ### for (i = 1 ; i <= ncommons ; i++) {
- ### print_arg(common[i])
- ### }
- ### print ")\n"
- ###
- ### # callees
- ###
- ### printf ";;; Calls:\n"
- ### printf " ()\n"
- ###
- ### # Local variables
- ###
- ### printf ";;; Local variables:\n"
- ### printf " ()\n"
- ###
- ### # Include files
- ### printf ";;; Includes:\n"
- ### printf " ()\n"
- ###
- ### # Externals
- ### printf ";;; Externals:\n"
- ### printf " ()\n"
- ###
- ### # close sexp
- ###
- ### print ")\n"
- ### }
- ###
-
- function clear_globals () {
- fcn_name = ""
- # Clear out array of parameters names
-
- for (i in param_names) delete param_names[i]
- for (i in param_nums) delete param_nums[i]
-
- # Clear out includes
- for (i in includes) delete includes[i]
- nincs = 0
-
- # Clear out externals
- for (i in externals) delete externals[i]
-
- # Clear out locals
- for (i in locals) delete locals[i]
-
- # Clear out common blocks
- for (c in common) delete common[c]
-
- # Clear out constants
- nconsts = 0
- for (i in const_names) {
- delete const_names[i]
- delete const_vals[i]
- }
-
- }
-
- BEGIN {
- first_time = 1
- IGNORECASE = 1
- }
-
-
- # parse function declaration line
- { if ($0 ~ /^ +SUBROUTINE/ ||
- $0 ~ /^ +REAL +FUNCTION/ ||
- $0 ~ /^ +REAL\*[0-9]+ +FUNCTION/ ||
- $0 ~ /^ +INTEGER +FUNCTION/ ||
- $0 ~ /^ +INTEGER\*[0-9]+ +FUNCTION/ ||
- $0 ~ /^ +LOGICAL +FUNCTION/ ||
- $0 ~ /^ +LOGICAL\*[0-9]+ +FUNCTION/ ||
- $0 ~ /^ +CHARACTER +FUNCTION/ ||
- $0 ~ /^ +CHARACTER\*[0-9]+ +FUNCTION/) {
-
-
- # if there already is a function parsed - flush collected info
-
- if (!first_time) {
-
- # print rest of fn's info
-
- print_fcn()
- }
-
- clear_globals()
-
- first_time = 0
-
- if ($0 ~ /^ +SUBROUTINE/) {
- firstarg = 4
- is_fn = 0
- }
- else {
- firstarg = 5
- is_fn = 1
- }
-
-
- # parse function info to array
-
- nfargs = split($0, param_names, /[ ,\(\)]+/) - 1
-
- fcn_name = param_names[firstarg-1]
-
- if (is_fn)
- fn_type = param_names[firstarg - 3]
-
-
- # compact array (remove all stuff except params names)
-
- for (i = firstarg; i <= nfargs; i++)
- param_names[i - firstarg + 1] = toupper(param_names[i])
-
- for (i = nfargs - firstarg + 2 ; i <= nfargs ; i++)
- delete param_names[i]
-
- nfargs -= firstarg - 1
-
- # The above preserves the order of the parameters - important.
- # However, we also need to be able to check if variables are
- # parameters, so we also create the inverse array:
- for (i in param_names)
- param_nums[param_names[i]] = i
- }
- # now parsing args declarations
- else if ($0 ~ /^ +REAL/ ||
- $0 ~ /^ +REAL\*[0-9]+/ ||
- $0 ~ /^ +INTEGER/ ||
- $0 ~ /^ +INTEGER\*[0-9]+/ ||
- $0 ~ /^ +LOGICAL/ ||
- $0 ~ /^ +LOGICAL\*[0-9]+/ ||
- $0 ~ /^ +CHARACTER/ ||
- $0 ~ /^ +CHARACTER\*[0-9]+/ ||
- $0 ~ /^ +%STRING%/ ) {
-
-
- gsub(",", " , ", $0)
- gsub(/\(/, " ( ", $0)
- gsub(/\)/, " ) ", $0)
- if ($0 ~ /^ +CHARACTER/) {
- gsub(/\*/, " * ", $0)
- }
-
- type = toupper($1)
-
- i = 2
- decllen = ""
- if ($i == "*") {
- i++
- if ($i == "(") {
- i++
- while (i <= NF && $i !~ /^\)$/) {
- decllen = decllen $i
- i++
- }
- }
- else {
- decllen = $i
- }
- i++
- }
-
- while (i <= NF) {
- pname = toupper($i)
- gsub(",", " ", pname)
- if (pname ~ /^ *$/) {
- i++
- }
- else {
-
- i++
-
- stmpa = " "
- stmpcharlen = decllen
- ## Handle dimensions
- if ($i ~ /^\($/) {
- stmpa = "(\042"
- i++
-
- while (i <= NF && $i !~ /^\)$/) {
- if ($i == ",") {
- stmpa = stmpa "\042 \042"
- }
- else {
- stmpa = stmpa $i " "
- }
- i++
- }
-
- stmpa = stmpa "\042)"
- i++
- }
- # Handle char vars otf foo*10, foo*(*), etc.
- if ($i == "*") {
- stmpcharlen = ""
- i++
- if ( $i == "(" ) {
- smtpcharlen = $i
- i++
-
- while (i <= NF && $i != ")") {
- stmpcharlen = stmpcharlen $i
- i++
- }
- i++
- }
- else {
- stmpcharlen = $i
- i++
- }
- }
- if ( stmpcharlen != "" ) {
- var_type[pname] = type "*" stmpcharlen
- var_dimen[pname] = stmpa
- }
- else {
- var_type[pname] = type
- var_dimen[pname] = stmpa
- }
- }
- }
- }
- }
-
- # Include file parsing:
- /^ +INCLUDE/ {
- nincs ++
- inc = $2
- gsub("'", "\042", inc)
- includes[nincs] = inc
- }
-
- # External stmt parsing
- /^ +EXTERNAL/ {
- gsub(",", "", $0);
- for (i=2; i <= NF; i++) {
- externals[toupper($i)] = 1
- }
- }
-
- # Is this a program instead of a subroutine?
- / +PROGRAM/ {
- fcn_name = "MAIN"
- }
-
- # Common block parsing:
- / +COMMON/ {
- gsub(/[,/]/, " ", $0);
- cbname= toupper($2)
- for (i=3; i <= NF; i++) {
- common[cbname] = common[cbname] " " toupper($i)
- }
- }
-
- # Parameter parsing:
- / +PARAMETER/ {
- gsub(/[,=()]/, " & ", $0)
- for (i=3; i<NF; i++) {
- p = $i # Get param name
- pv = ""
- for (i=(i+2); (i<NF && ($(i+2) != "=")); i++) { # Collect param values
- pv = pv $i
- }
- nconsts++
- const_names[nconsts] = p
- const_vals[nconsts] = pv
- }
- }
-
- # if there are non-printed parameters - print them
-
- END {
-
- print_fcn()
- }
-
- # end of $Source: /usr/bfr/src/test/RCS/fts-f2si.awk,v $
-